package com.jied.duodian.web;

import com.jied.duodian.entity.DdUser;
import com.jied.duodian.entity.Dutity.Page;
import com.jied.duodian.service.DdUserService;
import com.jied.duodian.service.Impl.AuthServiceImpl;
import com.jied.duodian.util.CommonResult;
import com.jied.duodian.util.SuId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/10/29
 * WWW https://github.com/j6l
 */
@RestController
@RequestMapping("/user")
@Slf4j
// @PreAuthorize("hasRole('ADMIN')")
public class userController {

    @Autowired
    DdUserService ddUserService;
    @Autowired
    AuthServiceImpl authService;
    @Autowired
    SuId suId;

    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();


    @GetMapping("/getRole")
    public CommonResult getuser(HttpServletRequest req) {
        DdUser user = ddUserService.getUserByName( suId.getuserName( req ) );
        return CommonResult.success( user.getRole() );
    }

    @GetMapping("/deluser/{id}")
    public CommonResult deluser(@PathVariable("id") Integer id) {
        Integer integer = ddUserService.delUser( id );
        return CommonResult.success( integer );
    }

    /**
     * 用户后台管理  分页获取
     *
     * @param page
     * @return
     */
    @PostMapping("/getall")
    public CommonResult getallpage(
            @RequestBody Page page) {
        DdUser ddUser = new DdUser();
        Page page1 = ddUserService.getUserLista( ddUser, page, 0 );
        log.info( "请求{}条数据,page为{}", page1.getContent().size(), page );
        return CommonResult.success( page );
    }

    /**
     * 封禁用户账户
     *
     * @param i
     * @param id
     * @return
     */
    @GetMapping("/jin/{i}/{id}")
    public CommonResult xiubyid(
            @PathVariable("i") Integer i,
            @PathVariable("id") Integer id) {
        DdUser ddUser = new DdUser();
        ddUser.setId( id );
        ddUser.setStatus( i );
        Integer integer = ddUserService.updataUserByid( ddUser );
        log.info( "修改了{}条数据  user  {}", integer, ddUser );
        return CommonResult.success( integer );
    }

    /**
     * 更新用户数据
     *
     * @param ddUser
     * @return
     */
    @PostMapping("/update")
    public CommonResult xiuuser(
            @RequestBody DdUser ddUser) {
        ddUser.setPassword( null );
        Integer integer = ddUserService.updataUserByid( ddUser );
        log.info( "修改了{}条数据  user  {}", integer, ddUser );
        return CommonResult.success( integer );
    }

    /**
     * 修改登陆者的密码
     *
     * @param passwd
     * @param oldpasswd
     * @param req
     * @return
     */
    @PostMapping("/updatapasswd/{passwd}/{oldpasswd}")
    public CommonResult updatapasswd(
            @PathVariable("passwd") String passwd,
            @PathVariable("oldpasswd") String oldpasswd,
            HttpServletRequest req) {
        DdUser user = ddUserService.getUserByName( suId.getuserName( req ) );
        if (encoder.matches( oldpasswd, user.getPassword() )) {
            user.setPassword( encoder.encode( passwd ) );
            Integer integer = ddUserService.updataUserByid( user );
            log.info( "修改了密码" );
            return CommonResult.success( "integer" );
        } else {
            return CommonResult.failed();
        }
    }


    /**
     * 添加用户
     *
     * @param ddUser
     * @return
     */
    @PostMapping("/adduser")
    public CommonResult adduser(
            @RequestBody DdUser ddUser) {
        System.out.println( ddUser );
        Integer integer = authService.register( ddUser );
        log.info( "添加了{}条数据  user  {}", integer, ddUser );
        return CommonResult.success( integer );
    }

    /**
     * 后台模糊查询用户列表
     *
     * @param page
     * @param pd   模糊查询的依据
     * @param id   模糊查询的内容
     * @return
     */
    @PostMapping("/getbyiduser/{pd}/{id}")
    public CommonResult getbyiduser(
            @RequestBody Page page,
            @PathVariable("pd") String pd,
            @PathVariable("id") String id) {

        Page lista = null;
        DdUser user = new DdUser();
        if ("nickname".equals( pd )) {
            user.setNickname( id );
            lista = ddUserService.getUserLista( user, page, 1 );
        }
        if ("username".equals( pd )) {
            user.setUsername( id );
            lista = ddUserService.getUserLista( user, page, 2 );
        }
        if ("phone".equals( pd )) {
            user.setPhone( id );
            lista = ddUserService.getUserLista( user, page, 3 );
        }
        log.info( "查询了{}条数据", lista.getContent().size() );
        return CommonResult.success( lista );
    }


}
